package com.makenv.web.backend;

import com.makenv.domain.Msg;
import com.makenv.service.MsgService;
import com.makenv.util.Config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("/manage/msg")
public class MsgManageController {

    @Autowired
    MsgService msgService;

    //每次显示的数据
    private int row;

    //总的页数
    private int pageSum;

    @RequestMapping("/showback")
    public String showBack(HttpServletRequest request) {

        row = Config.getInt("showRow");
        int totalMsg = msgService.getMsgSum();
        pageSum = 0;
        if (totalMsg % row == 0) {
            pageSum = totalMsg / row;
        } else {
            pageSum = totalMsg / row + 1;
        }
        request.getSession().setAttribute("msgPageSum",pageSum);
        request.getSession().setAttribute("msgPageNum",1);
        List<Msg> msgList = msgService.showMsgList(0, row);
        request.setAttribute("msgList", msgList);
        return "back/message";
    }

    @RequestMapping("/delete")
    public String delete(@RequestParam String jsonStr) {

        String[] idArray = jsonStr.split(",");
        for (String str : idArray) {
            int i = Integer.valueOf(str);
            msgService.deleteByPrimaryKey(i);
        }

        //这个请求是由ajax发送上来的，SpringMVC不会进行页面的跳转，用ajax进行跳转
        return "";
    }

    @RequestMapping("/jump")
    public String jump(@RequestParam int pagenum, HttpServletRequest request) {

        request.getSession().setAttribute("msgPageSum",pageSum);
        request.getSession().setAttribute("msgPageNum",pagenum);
        List<Msg> msgList = msgService.showMsgList((pagenum - 1) * row, row);
        request.setAttribute("msgList", msgList);
        return "back/message";
    }
}
